﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Gqq.Csharpindepth.Common
{
   public class XmlCommon
    {
       /// <summary>
       /// 读取Xml文件中的内容。
       /// </summary>
       public static void ReadXml(string path = "Common\\DVDList.xml")
       {
           XDocument xd = XDocument.Load(path);
           StringBuilder sb = new StringBuilder();
           var xElement = xd.Element("DvdList");
           if (xElement != null)
               foreach (XElement xe in xElement.Elements())
               {
                   sb.AppendLine(xe.Element("Title").ToString());
                   sb.AppendLine(xe.Element("Director").ToString());
                   sb.AppendLine(xe.Element("Price").ToString());
                   sb.AppendLine(xe.Element("Starring").ToString());
               }
           else
               sb.AppendLine("文件错误");
           Console.Write(sb.ToString());
       }

       public static void ReadXmlWithLinq(string path = "Common\\DVDList.xml")
       {
           XDocument xd = XDocument.Load(path);
           var query = from dvd in xd.Descendants("DVD")
                       where (int) dvd.Attribute("ID") > 0
                       select new {Title = (string)dvd.Element("Title"), Director = (string)dvd.Element("Director")};
           foreach (var s in query)
           {
//               Console.WriteLine(s);
               Console.WriteLine("{0}_{1}", s.Title, s.Director);
           }
       }
    }
}
